﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DAL;
using DAL.Managers;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Opds_Viewer.Control;
using Opds_Viewer.Model;
using Opds_Viewer.View;

namespace Opds_Viewer.ViewModel
{
    public class HomeViewModel : ViewModelBase
    {
        #region Private fields

        private MyCatalogs _oCatalogs;
        private MyCatalog _selectCatalog;
        
        // Commanding
        private readonly RelayCommand _consultCatalogCommand;
        private readonly RelayCommand _addCatalogCommand;

        #endregion

        #region Public fields

        public MyCatalog NewCatalog;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="HomeViewModel"/> class.
        /// </summary>
        public HomeViewModel()
        {
            // Init the commands
            _addCatalogCommand = new RelayCommand(ShowAddCalatlogDialog);
            _consultCatalogCommand = new RelayCommand(ConsultCatalog, CanConsult);
            
            OCatalogs = MyCatalogs.Instance;
        }

        #region Commanding

        /// <summary>
        /// Command to add a new MyCatalog
        /// </summary>
        public ICommand AddCatalogCommand
        {
            get { return _addCatalogCommand; }
        }

        /// <summary>
        /// Command to change view to consult a selected catalog
        /// </summary>
        public ICommand ConsultCatalogCommand
        {
            get { return _consultCatalogCommand; }
        }

        #endregion

        #region public properties

        /// <summary>
        /// Current page (view) displayed in the central content presenter
        /// </summary>
        public MyCatalogs OCatalogs
        {
            get { return _oCatalogs; }
            private set
            {
                _oCatalogs = value;
                RaisePropertyChanged("OCatalogs");
            }
        }

        /// <summary>
        /// Gets or sets the selected catalog.
        /// </summary>
        /// <value>The selected catalog.</value>
        public MyCatalog SelectedCatalog
        {
            get { return _selectCatalog; }
            set
            {
                _selectCatalog = value;
                RaisePropertyChanged("SelectedCatalog");
            }
        }

        #endregion

        #region private functions

        private void ShowAddCalatlogDialog()
        {
            NewCatalog = null;
            var dialog = new AddCatalogBox(ref NewCatalog);
            dialog.ShowDialog();
        }

        /// <summary>
        /// Determines whether we can consult a catalog.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if we can consult; otherwise, <c>false</c>.
        /// </returns>
        private bool CanConsult()
        {
            return SelectedCatalog != null && SelectedCatalog.Status;
        }

        /// <summary>
        /// Change the view to consult the catalog.
        /// </summary>
        private void ConsultCatalog()
        {
            ((ViewModelLocator) Application.Current.Resources["Locator"]).Main.CurrentPage = new CatalogView();
            ((ViewModelLocator) Application.Current.Resources["Locator"]).Catalog.CurrentCatalog = this.SelectedCatalog;
        }

        #endregion
    }
}
